package com.cgbl.cg.vm.main

import android.app.Application
import androidx.databinding.ObservableField
import api.CourseServiceFactory
import api.ResourceServiceFactory
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.configs.AppConstants
import com.robot.base.configs.PrefsManager
import com.robot.base.model.*
import com.robot.base.util.arouter.ARouterUtil
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import me.goldze.mvvmhabit.http.BaseResponse

class ShopFragmentViewModel(application: Application) : GLBaseViewModel(application) {

    val uc = UiChangeEvent()
    val vip = ObservableField(false)
    val avatar = ObservableField<String>()
    val name = ObservableField<String>()
    val time = ObservableField<String>()

    class UiChangeEvent {
        val recommendCourseDataEvent = SingleLiveEvent<List<CourseBean>>()
        val allCourseDataEvent = SingleLiveEvent<List<CourseBean>>()
        val recommendResourceDataEvent = SingleLiveEvent<List<ResourceBean>>()
        val allResourceDataEvent = SingleLiveEvent<List<ResourceBean>>()
    }

    val clickOpenVipCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.Mine.A_MINE_OPEN_VIP)
    })

    val clickSearchCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.HOME.A_SEARCH)
    })

    fun initUserInfo() {
        val user = PrefsManager.getUserInfo()
        avatar.set(user.head)
        name.set(user.nickName)
        vip.set(user.vipType == "1")//
    }

    fun initData() {
        getRecommendCourse()
        getAllCourse()
        getAllResource()
        getRecommendResource()
    }

    fun getRecommendCourse() {
        CourseServiceFactory.page(
            mapOf(
                "sortType" to 0,
                "pageNo" to 1,
                "pageSize" to 4,
                "classRecommend" to 3,
                "isAccount" to 0,
                "isFree" to 2
            )
        ).subscribe(object :
            AbstractViewModelSubscriber<BaseResponse<PageListBean<CourseBean>>>(this) {
            override fun onNext(t: BaseResponse<PageListBean<CourseBean>>) {
                uc.recommendCourseDataEvent.postValue(t.data.list)
            }
        })
    }

    fun getAllCourse() {
        CourseServiceFactory.page(
            mapOf(
                "sortType" to 0, "pageNo" to 1, "pageSize" to 4, "isAccount" to 0, "isFree" to 2
            )
        ).subscribe(object :
            AbstractViewModelSubscriber<BaseResponse<PageListBean<CourseBean>>>(this) {
            override fun onNext(t: BaseResponse<PageListBean<CourseBean>>) {
                uc.allCourseDataEvent.postValue(t.data.list)
            }
        })
    }

    fun getRecommendResource() {
        ResourceServiceFactory.page(
            mapOf(
                "naturalRecommend" to 4,
                "isFree" to 2,
                "pageNo" to 1,
                "pageSize" to 4,
                "isAccount" to 0,
                "sort" to 1
            )
        ).subscribe(object :
            AbstractViewModelSubscriber<BaseResponse<PageListBean<ResourceBean>>>(this) {
            override fun onNext(t: BaseResponse<PageListBean<ResourceBean>>) {
                uc.recommendResourceDataEvent.postValue(t.data.list)
            }
        })
    }

    fun getAllResource() {
        ResourceServiceFactory.page(
            mapOf(
                "isFree" to 2, "pageNo" to 1, "pageSize" to 4, "isAccount" to 0, "sort" to 1
            )
        ).subscribe(object :
            AbstractViewModelSubscriber<BaseResponse<PageListBean<ResourceBean>>>(this) {
            override fun onNext(t: BaseResponse<PageListBean<ResourceBean>>) {
                uc.allResourceDataEvent.postValue(t.data.list)
            }
        })
    }
}